var http = require("http");
var url = require("url");

function start(route, handle) {
    http.createServer(function(request, response) {
        var pathname = url.parse(request.url).pathname;
        console.log("received request for " + pathname);

        request.setEncoding("utf8");
        var postData = "";
        
        request.addListener("data", function(postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk" + postDataChunk);
        });

        request.addListener("end", function() {
            route(pathname, handle, response, request, postData);
        });
    }).listen(8888);

    console.log("Server has started at port 8888");
}

exports.start = start;
